package com.vzaar.examples;

import java.util.List;

import com.vzaar.Video;
import com.vzaar.VideoListRequest;
import com.vzaar.Vzaar;
import com.vzaar.VzaarException;

/**
 * Example showing the video list command.
 * 
 * @author Marc G. Smith
 */
public class VideoListExample 
{
	///////////////////////////////////////////////////////////////////////////

	public static void main(String[] args) {
		try {
			ParameterHelper helper = new ParameterHelper(args);
			args = helper.getRemainingArgs();
			if(args.length > 2) {
				printUsage();
				System.exit(1);
				return;
			}
			
			String titleSearch = null;
			if(args.length == 2) {
				titleSearch = args[1];
			}

			Vzaar vzaar = helper.createVzaar();
			
			VideoListRequest request = new VideoListRequest(args[0]);
			request.setPage(1);
			request.setSize(true);
			request.setCount(100);
			request.setSortAscending(true);
			request.setTitleSearch(titleSearch);
			
			List<Video> videoList = null;
			while(!(videoList = vzaar.getVideoList(request)).isEmpty()) 
			{
				for(Video video : videoList) {
					System.out.println(
						"ID=" + video.getId() +
						", Created=" + video.getCreatedAt() +
						", Size=" + video.getWidth() + "x" + video.getHeight() +
						", Duration=" + video.getDuration() + "s" +
						", Play Count=" + video.getPlayCount() +
						", URL=" + video.getUrl() +
						", Title=" + video.getTitle());
				}
				
				if(videoList.size() < 100) break;
				request.setPage(request.getPage() + 1);
			}
		}
		catch(VzaarException e) {
			System.err.println("Error: " + e.getMessage());
			System.exit(2);
		}
		catch(ArrayIndexOutOfBoundsException e) {
			printUsage();
			System.exit(1);
		}
		catch(NumberFormatException e) {
			printUsage();
			System.exit(1);
		}
	}
	
	///////////////////////////////////////////////////////////////////////////

	private static final  void printUsage() {
		System.out.println("Usage: vzaar-list " +
			ParameterHelper.getCommonCommandLineArgs() + 
			" <username> [<title-search>]\n");
		System.out.println("Params:");
		System.out.println("   <username>          " +
		"The user name to fetch active videos for");
		System.out.println("   <title-search>      " +
			"Return only videos with title containing given string");
		System.out.println(ParameterHelper.getCommonCommandLineHelp());
	}
	
	///////////////////////////////////////////////////////////////////////////
}
